package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import com.sky.utils.QnyOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import static java.util.UUID.randomUUID;

/**
 * 文件上传控制类
 */
@RestController
@RequestMapping("/admin/common")
@Slf4j
@Api(tags = "通用接口——文件上传")
public class CommonController {

    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private QnyOssUtil qnyOssUtil;
    @PostMapping("/upload")
    @ApiOperation("上传文件")
    public Result<String> upload(MultipartFile file) throws IOException {
        log.info("文件上传:{}",file);
        // 1、获取文件名
        String originalFilename = file.getOriginalFilename();
        // 2、截取文件名后缀
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 3、生成文件名
        String UUID = randomUUID().toString();
        // 4、拼接文件名
        String ObjectName = UUID + extension;
        log.info("文件名:{}",UUID);
        // 5、上传文件到OSS
//        String FilePath = aliOssUtil.upload(file.getBytes(), ObjectName);
        String FilePath = qnyOssUtil.upload2Qiniu(file.getBytes(), ObjectName);
        return Result.success(FilePath);
    }
}
